/*
 * @Author: wjk
 * @Date: 2020-01-13 16:59:29
 * @LastEditors: wjk
 * @LastEditTime: 2021-02-06 11:08:53
 * @description: file content
 */
import Vue from 'vue'
import Vuex from 'vuex'
import { mineApi, commissionApi,memberApi } from '@/apis'
import { sensorRegister } from '../plugins/sensor'
import { appConfig } from '@/utils/tools'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    systemInfo: {
      statusBarHeight: 20,
      screenWidth: 375,
      screenHeight: 667,
      SDKVersion: '',
      navHeight: 44
    },
    couponTemp: false,
    token: uni.getStorageSync('biXuanHello'),
    userInfo: uni.getStorageSync('biXuanWorld') || {},
    goodsTempObj: null,
    setting: uni.getStorageSync('biXuanSetting') || {},
    showPopup: false,
    extraData: null, // 用于接收其他小程序传过来的参数
    optionState: [] ,// 选中商品数据
  },
  mutations: {
    setSystemInfo: (state, data) => {
      state.systemInfo = data
    },
    setToken: (state, data) => {
      state.token = data
      if (data) {
        uni.setStorage({
          key: 'biXuanHello',
          data
        })
      } else {
        uni.removeStorage({
          key: 'biXuanHello'
        })
      }
    },
    setUserInfo: (state, data) => {
      state.userInfo = data
      if (data) {
        uni.setStorage({
          key: 'biXuanWorld',
          data
        })
        sensorRegister({ uid: data.uid, gender: data.gender, mobile: data.mobile })
      } else {
        uni.removeStorage({
          key: 'biXuanWorld'
        })
      }
    },
    setUserInfoKey: (state, map) => {
      for (const key in map) {
        state.userInfo[key] = map[key]
      }
      uni.setStorage({
        key: 'biXuanWorld',
        data: state.userInfo
      })
    },
    SET_COUPON_TEMP: (state, temp) => {
      state.couponTemp = temp
    },
    SET_GOODS_TEMP: (state, object) => {
      state.goodsTempObj = object
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    setShowPopup: (state, flag) => {
      state.showPopup = flag
    },
    SET_EXTRADATA: (state, data) => {
      state.extraData = data
    },
    PRODUCT_NUMBER: (state, data) => {
      if (data.isCheck === true) {
        state.optionState.push(data)
        state.optionState = state.optionState.map(res => {
          // res.id = null
          res.goodsImageUrl = res.pic
          res.goodsName = res.goodsTitle
          res.marketPrice = res.price
          return res
        })
      } else {
        state.optionState.splice(state.optionState.indexOf(data.id), 1)
      }
    },
    DELETE_LIST: (state, data) => {
      state.optionState.forEach((res, index) => {
        if (res.id === data.id) {
          state.optionState.splice(index, 1)
        }
      })
    },
    PRODUCT_DATA: (state, data) => {
      state.optionState = data.map(item => {
        item.goodsId = item.goods_id
        item.marketPrice = item.price
        return item
      })
    },
    SET_PUBULISHDATA: (state, data) => {
      state.optionState = data
    }
  },
  actions: {
    SET_SYSTEM_INFO({ commit }, data) {
      commit('setSystemInfo', data)
    },
    SET_TOKEN({ commit }, token) {
      commit('setToken', token)
    },
    SET_USER({ commit,dispatch }, data) {
      uni.$emit('login', {
        type: 'SET_USER'
      })
      commit('setToken', data.token)
      commit('setUserInfo', data.userInfo)
    },
    SET_USER_INFO({ commit }, data) {
      commit('setUserInfoKey', data)
    },
    LOGOUT_USER({ commit }, data) {
      commit('setToken', '')
      commit('setUserInfo', '')
      uni.removeStorageSync('share_code')//去除邀请码
    },
    setGoodsTemp({ commit }, object) {
      commit('SET_GOODS_TEMP', object)
    },
    setExtraData({ commit }, data) {
      commit('SET_EXTRADATA', data)
    },
    RETURN_PUBLISH: ({ commit }, data) => {
      commit('SET_PUBULISHDATA', data)
    },
    // 获取用户信息
    async GetUserInfo({ commit, state }) {
      await appConfig()
      const res = await mineApi.getMy({
        hideLoading: true
      })
      if (res.success) {
        console.log('my:',res);
        const user = res.data.user
        const ress = await commissionApi.getLevelMy({
          hideLoading: true,
          userId: user.uid
        })
        if (ress.code === '000000') {
          user.referral_code = ress.data.recommendRealName
          user.parentMobile = ress.data.parentMobile
          user.parentId = ress.data.parentId
          user.parentNickname = ress.data.parentNickname
          if (user) {
            commit('setUserInfo', Object.assign({}, this.state.userInfo, user))
            commit('SET_SETTING', res.data)
            uni.setStorage({
              key: 'biXuanSetting',
              data: res.data
            })
          }
        } else {
          if (user) {
            commit('setUserInfo', user)
            commit('SET_SETTING', res.data)
            uni.setStorage({
              key: 'biXuanSetting',
              data: res.data
            })
          }
        }
      }
    },
  },
  getters: {
    goodsTempObj(state) {
      return state.goodsTempObj
    },
    prePageTitle() {
      return ''
    },
    preSeat() {
      return ''
    },
    user(state) {
      return state.userInfo
    },
    userToken(state) {
      return state.token
    },
    setting(state) {
      return state.setting
    },
    extraData(state) {
      return state.extraData
    },
  }
})

export default store
